package tv.bem {
    import flash.external.ExternalInterface;
    import flash.display.*;
    import flash.utils.setTimeout;
    import flash.events.*;
    import org.mangui.hls.utils.*;
    import flash.geom.Rectangle;
    import flash.system.Security;
	import tv.utils.tvContextMenu;

    import com.globo.Player;
    import tv.bem.BemTVURLStream;
    import tv.bem.PlaybackIdHolder;

    CONFIG::LOGGING {
        import org.mangui.hls.utils.Log;
    }

    public class BemTVPlayer extends Player {
        private var idHolder:PlaybackIdHolder;

        public function BemTVPlayer() {
            super();
            idHolder = PlaybackIdHolder.getInstance();
            idHolder.playbackId = LoaderInfo(this.root.loaderInfo).parameters.playbackId;
			ctMenu = new tvContextMenu;
			this.contextMenu = ctMenu.contextMenu;
        }
		
		private var ctMenu:tvContextMenu;

        override protected function _setupExternalGetters():void {
            super._setupExternalGetters();
            ExternalInterface.addCallback("getmaxBufferLength", _getmaxBufferLength);
            ExternalInterface.addCallback("getminBufferLength", _getminBufferLength);
            ExternalInterface.addCallback("getlowBufferLength", _getlowBufferLength);
            ExternalInterface.addCallback("getDelay", _getDelay);
        }

        override protected function _setupExternalCallers():void {
            super._setupExternalCallers();
            ExternalInterface.addCallback("playerSetmaxBufferLength", _setmaxBufferLength);
            ExternalInterface.addCallback("playerSetminBufferLength", _setminBufferLength);
            ExternalInterface.addCallback("playerSetlowBufferLength", _setlowBufferLength);

            ExternalInterface.addCallback("playerSetJSURLStream", _setJSURLStream);
            ExternalInterface.addCallback("playerResourceLoadError", _resourceLoadError);
            
        }

        override protected function _onStageVideoState(event : StageVideoAvailabilityEvent) : void {
            super._onStageVideoState(event);
            _hls.URLstream = BemTVURLStream as Class;
        }

        override protected function _setJSURLStream(jsURLstream : Boolean) : void { 
            if (jsURLstream) {
                CONFIG::LOGGING {
                    Log.soha("--------set BemTVURLStream------")
                }
                _hls.URLstream = BemTVURLStream as Class;
            } else {
                CONFIG::LOGGING {
                    Log.soha("--------set URLStream------")
                }
                super._setJSURLStream(false);
            }
        }

        protected function _resourceLoadError(): void{
            CONFIG::LOGGING {
                Log.soha("--------resource load error------")
            }
        }

	 private function _getDelay():Number {
	     return _hls.delay;
	 }
    }
}
